/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.groups;

import framework.dal.GroupDB;
import framework.dal.ItemDB;
import javax.swing.JOptionPane;

/**
 *
 * @author Guilherme
 */
public class NavieGroupManager extends framework.groups.GroupStrategy
{
    
    @Override
    public boolean addItem(Item m)
    {
        Integer id = m.getIdentifier();
        if (id == null)
        {
            id = GroupStrategy.GROUPABLE.getGroupSelected();
            if (id == null || id == -1)
            {
                JOptionPane.showMessageDialog(null, "Ops! Alguma coisa não está certa! :(");
                return false;
            }
        }
        m.setIdentifier(ItemDB.getLastInteger());
        return ItemDB.add(m, id);
    }

    @Override
    public boolean removeItem(Item m)
    {
        return ItemDB.remove(m);
    }

    @Override
    public boolean moveItem(Integer idGroupSource, Integer idGroupDestination, Item m)
    {
        return false;
    }

    @Override
    public String createGroup()
    {
        String s = JOptionPane.showInputDialog("Qual o nome do novo grupo?");
        Group g = new Group(GroupDB.getLastInteger() + 1, s);
        if (GroupDB.insert(s, (GroupDB.getLastInteger() + 1)))
        {
            GroupStrategy.GROUPS.add(g);
            GroupStrategy.GROUPABLE.rePaintGroups();
            return s;
        }
        else
        {
            return null;
        }
    }

    @Override
    public boolean removeGroup(String nameGroup)
    {
        if (nameGroup == null)
        {
            JOptionPane.showMessageDialog(null, "Selecione um grupo para removê-lo");
            return false;
        }
        if (GroupDB.deleteGroup(nameGroup))
        {
            GroupStrategy.GROUPABLE.rePaintGroups();
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean alterarItem(Item m)
    {
        return ItemDB.update(m);
    }
}
